﻿using BusinessLayer;
using MongoDB.Bson;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Linq;
using MvcOSGi.Shell.App_Start;
using MvcOSGi.Shell.Infra;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcOSGi.Shell.ActionFilter
{
    [AttributeUsage(AttributeTargets.Method)]
    public class FillVersionAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var controller = filterContext.Controller as BaseController;

            if (controller == null)
            {
                throw new InvalidOperationException("It is not BaseController !!!");
            }

            controller.RequestInfo = new RequestInfo(HttpContext.Current.Request);


            MultipleResultsBag BaseData = new MultipleResultsBag();
            BaseData.Add(controller.RequestInfo);
            //controller.DataBag.Add(BusinessLayer.Enums.DataBagKey.BaseData, VersionData);
            //DB call to get version
            IQueryable<Version.Version> versions = controller.Context.Versions.AsQueryable();
            IQueryable<Company.Company> companies = controller.CompanyContext.Domains.AsQueryable();
            Version.Version currentVersion = new Version.Version();
            Version.Page currentPage = new Version.Page();
            Company.Company company = new Company.Company();

            if (controller.RequestInfo.CID != 0)
            {
                var query = Query<Company.Company>.EQ(p => p.CID, controller.RequestInfo.CID);
                company = companies.Where(r => query.Inject()).First();
            }
            else
            {
                throw new Exception("CID is null");
            }

            if (!controller.RequestInfo.VersionID.HasValue)
            {
                var query = Query<Version.Version>.EQ(p => p.IsDefault, true);
                currentVersion = versions.Where(r => query.Inject()).First();
            }
            else
            {
                var query = Query<Version.Version>.EQ(p => p.VersionID, controller.RequestInfo.VersionID);
                currentVersion = versions.Where(r => query.Inject()).First();
            }

            currentPage = currentVersion.Pages.Where(p => p.PageName.Equals(controller.RequestInfo.RequestPageType, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
            if (string.IsNullOrEmpty(currentPage.VersionHTML))
            {
                currentPage.VersionHTML = currentVersion.VersionHTML;
            }
            if (currentPage.FileIncludes == null)
            {
                if (currentVersion.FileIncludes != null)
                    currentPage.FileIncludes = new List<Version.Include>(currentVersion.FileIncludes);
            }

            BaseData.Add(currentVersion);
            BaseData.Add(currentPage);
            BaseData.Add(company);
            controller.DataBag.Add(BusinessLayer.Enums.DataBagKey.BaseData, BaseData);
            //controller.VersionInfo = currentVersion;
            //controller.PageInfo = currentPage;
            base.OnActionExecuting(filterContext);
        }
    }
}